#include <iostream>
#include "ALLMenu.h"
#include "Show.h"
#include "data.h"
#include "Archive.h"

Show::Show(MainMenu* M) {
	Menu = M;
}

Show::Show(PlayMenu01 *M) {
	DemoValue = M->GetDemoValue();
	Menu = M;
}


Show::Show(LoadMenu *M) {
	Menu = M;
}

Show::Show(PauseMenu *M) {
	Menu = M;
}

Show::Show(OptionMenu *M) {
	Menu = M;
}

Show::Show(SetVolumeMenu *M) {
	Menu = M;
}

Show::Show(SetResolutionMenu *M) {
	Menu = M;
}

Show::Show(SaveMenu *M) {
	Menu = M;
}

void Show::Display() {
	system("cls");
	if(DemoValue) {
		std::cout << "The game is running.\n";
		std::cout << "Current Volume: " << Data::getInstance().vol() << std::endl;
		std::cout << "Resolution: " << Data::getInstance().res(Data::getInstance().Res()) << std::endl;
		std::cout << "DemoValue: " << DemoValue << std::endl;
	}
	std::cout << "\n-------------------------------\n";
	std::cout << "\t" << Menu->GetMyName() << std::endl;
	for(int i = 1; i < Menu->GetItem(); i++) {
		std::cout << i << ". " << Menu->GetChildName(i) << std::endl;
	}
	std::cout << 0 << ". " << Menu->GetChildName(0) << std::endl;

}

int Choose::Option(IMenu *m, int num) {
	if(num == 0) {
		num = m->GetItem();
	}
	std::cout << "Plz choose an option above.\n";
	std::string a;
	std::cin >> a;
	std::cin.clear();
	int len = a.length();
	if(len > num / 10 + 1) {
		std::cout << "Error. Plz choose again.\n";
		system("pause");
		return -1;
	}
	int xch = 0;
	for(int i = 0; i < len; i++) {
		if(a[i] < '0' || a[i] > '9') {
			std::cout << "Error. Plz choose again.\n";
			system("pause");
			return -1;
		}
		xch = xch * 10 + a[i] - '0';
	}
	if(xch >= num) {
		std::cout << "Error. Plz choose again.\n";
		system("pause");
		return -1;
	}
	return xch;
}

void Show::SetVolume() {
	system("cls");
	std::cout << "\n-------------------------------\n";
	std::cout << "\t" << Menu->GetMyName() << std::endl;
	std::cout << "Current Volume: " << Data::getInstance().vol() << std::endl;
}

int Choose::SetVolume() {
	std::cout << "Plz input a number in [0, 100]: ";
	std::string a;
	std::cin >> a;
	std::cin.clear();
	if(a.length() > 3) {
		std::cout << "Error. Plz input again.\n";
		system("pause");
		return -1;
	}
	int xch = 0;
	for(unsigned int i = 0; i < a.length(); i++) {
		if(a[i] < '0' || a[i] > '9') {
			std::cout << "Error. Plz input again.\n";
			system("pause");
			return -1;
		}
		xch = xch * 10 + a[i] - '0';
	}
	if(xch > 100) {
		std::cout << "Error. Plz input again.\n";
		system("pause");
		return -1;
	}
	return xch;
}


void Show::SetResolution() {
	system("cls");
	std::cout << "\n-------------------------------\n";
	std::cout << "\t" << Menu->GetMyName() << std::endl;
	for(int i = 1; i <= 3; i++) {
		std::cout << i << ". " << Data::getInstance().res(i);
		if(i == Data::getInstance().Res()) {
			std::cout << "(*)";
		}
		std::cout << std::endl;
	}
	std::cout << "0. Back" << std::endl;
}

int Choose::SetResolution() {
	std::cout << "Plz choose an option above: ";
	std::string a;
	std::cin >> a;
	std::cin.clear();
	if(a.length() > 1) {
		std::cout << "Error. Plz choose again.\n";
		system("pause");
		return -1;
	}
	int xch = 0;
	for(unsigned int i = 0; i < a.length(); i++) {
		if(a[i] < '0' || a[i] > '9') {
			std::cout << "Error. Plz choose again.\n";
			system("pause");
			return -1;
		}
		xch = xch * 10 + a[i] - '0';
	}
	if(xch > 3) {
		std::cout << "Error. Plz choose again.\n";
		system("pause");
		return -1;
	}
	return xch;
}

void Show::Save() {
	system("cls");
	std::cout << "\n-------------------------------\n";
	std::cout << "\t" << Menu->GetMyName() << std::endl;
	for(int i = 1; i < Menu->GetItem(); i++) {
		std::cout << i << ". " << Menu->GetChildName(i) << std::endl;
	}
	std::cout << Menu->GetItem() << ". " << "New Record" << std::endl;
	std::cout << 0 << ". " << Menu->GetChildName(0) << std::endl;
}

std::string Choose::Save() {
	std::cout << "Plz input a file name: ";
	std::string a;
	std::cin >> a;
	std::cin.clear();
	return a;
}

void Show::Archive() {
	system("cls");
	std::cout << "\n-------------------------------\n";
	std::cout << "\t" << Menu->GetMyName() << std::endl;
	for(int i = 0; i < Archives::getInstance().total(); i++) {
		std::cout << i + 1 << ". " << Archives::getInstance().getArchiveName(i) << std::endl;
	}
	std::cout << "0. Back" << std::endl;
}

